
package classes.view.component
{
	import classes.controller.DAOSendFlash;
	import classes.controller.DAOSendJS;
	import classes.controller.EClick;
	import classes.controller.ESendJS;
	import classes.controller.VONullObject;
	import classes.model.VOData;
	import classes.model.VOIsLocal;
	import classes.model.VOUserInfo;
	
	import com.d4p.custom.defaultButton.DefaultBtn;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class InfoBar extends Sprite
	{
		//info text & icon
		private var _name:TextField;
		private var _level:MovieClip;
		private var _score:TextField;
		private var _win:TextField;
		private var _lose:TextField;
		private var _grade:TextField;
		private var _ranking:TextField;
		private var _ranking_grade:TextField;
		private var _ranking_icon:MovieClip;
		private var _friend_on:MovieClip; 
		
		//info btn (우측)
		private var _infoBtn:DefaultBtn;	//성적분석 
		private var _friendBtn:DefaultBtn;	//친구관리 
		private var _settingBtn:DefaultBtn; //설정 
		
		public function InfoBar()
		{
			super();
			
			setLayOut();
			defaultSetting();
		}
		
		public function set infoBarSwitch($switch:Boolean):void
		{
			if($switch){
				init_data();
				initListener();
			}else{
				removeListener();
				removeData();
			}
		}
		
		public function set infobarUtilSwitch($switch:Boolean):void
		{
			this._infoBtn.mouseChildren	 	= $switch;
			this._friendBtn.mouseChildren 	= $switch;
			this._settingBtn.mouseChildren	= $switch;
			this._infoBtn.mouseEnabled 		= $switch;
			this._friendBtn.mouseEnabled 	= $switch;
			this._settingBtn.mouseEnabled 	= $switch;
			
			if($switch){
				this._infoBtn.alpha				= 1;
				this._friendBtn.alpha			= 1;
				this._settingBtn.alpha			= 1;
				this._friend_on.visible			= true;
			}else{
				this._infoBtn.alpha				= 0.4;
				this._friendBtn.alpha			= 0.4;
				this._settingBtn.alpha			= 0.4;
				this._friend_on.visible			= false;
			}
			
			
			
		}
		
		private function setLayOut():void
		{
			this._name			= this.getChildByName('name_text')			as TextField;
			this._level 		= this.getChildByName('level_mc')			as MovieClip;
			this._score 		= this.getChildByName('score_text')			as TextField;
			this._win			= this.getChildByName('win_text')			as TextField;
			this._lose			= this.getChildByName('lose_text')			as TextField;
			this._grade			= this.getChildByName('grade_text')			as TextField;
			this._ranking		= this.getChildByName('ranking_text')		as TextField;
			this._ranking_grade	= this.getChildByName('ranking_g_text')		as TextField;
			this._ranking_icon	= this.getChildByName('ranking_icon')		as MovieClip;
			this._friend_on		= this.getChildByName('friend_on_mc')		as MovieClip; 
			
			this._infoBtn		= this.getChildByName('info_btn')			as DefaultBtn;
			this._friendBtn		= this.getChildByName('friend_btn')			as DefaultBtn;
			this._settingBtn	= this.getChildByName('setting_btn')		as DefaultBtn;
		}
		
		private function defaultSetting():void
		{
			var textformat_letterspacing:TextFormat = new TextFormat();
			textformat_letterspacing.letterSpacing = -0.7;
			this._level.stop();
			this._ranking_icon.stop();
			
			this._name.setTextFormat(textformat_letterspacing);
			this._score.setTextFormat(textformat_letterspacing);
			this._win.setTextFormat(textformat_letterspacing);
			this._lose.setTextFormat(textformat_letterspacing);
			this._grade.setTextFormat(textformat_letterspacing);
			this._ranking.setTextFormat(textformat_letterspacing);
			this._ranking_grade.setTextFormat(textformat_letterspacing);
			this._level.level_txt.setTextFormat(textformat_letterspacing);
		}
		
		
		
		private function initListener():void
		{
			this._infoBtn.addEventListener(MouseEvent.CLICK,		onClickHandler);
			this._friendBtn.addEventListener(MouseEvent.CLICK,		onClickHandler);
			this._settingBtn.addEventListener(MouseEvent.CLICK,		onClickHandler);
		}
		
		private function removeListener():void
		{
			this._infoBtn.removeEventListener(MouseEvent.CLICK,		onClickHandler);
			this._friendBtn.removeEventListener(MouseEvent.CLICK,	onClickHandler);
			this._settingBtn.removeEventListener(MouseEvent.CLICK,	onClickHandler);
		}
		
		private function init_data():void
		{
			if(VOIsLocal.getLocalInstance().isLocal == "FILE"){
				//test.
				dataSetting = VOUserInfo.getDataInstance().data;
			}else{
				//UserInfo가 바뀔 때마다.
				DAOSendJS.getLoadInstance().addEventListener(ESendJS.EVENT_USERINFO,	onRoomInfoLoadcomplete);	
			}
		}
		
		private function onRoomInfoLoadcomplete(e:ESendJS):void
		{
			var obj:Object 	= e._obj;
			dataSetting 	= obj;
		}
		
		private function set dataSetting($obj:Object):void
		{
			this._name.text 			= VONullObject.getLoadInstance().nullNickText($obj.nick);
			this._level.level_txt.text 	= VONullObject.getLoadInstance().nullText($obj.level);
			var levelNum:int;
			if($obj.level != null){
				levelNum = Math.floor(Number($obj.level)/10)+1;
				this._level.gotoAndStop(levelNum);
			}
			
			this._score.text 			= VONullObject.getLoadInstance().nullNickText($obj.nowScore);
			this._win.text 				= VONullObject.getLoadInstance().nullNickText($obj.win);
			this._lose.text 			= VONullObject.getLoadInstance().nullNickText($obj.lose);
			this._grade.text 			= VONullObject.getLoadInstance().nullNickText($obj.grade);
			this._ranking.text			= VONullObject.getLoadInstance().nullNickText($obj.nowRank);
			
			if(($obj.nowRank != null) && ($obj.nowRank != null)){
				var rankingState:int = $obj.nowRank - $obj.beforeRank;
				this._ranking_grade.text = String(Math.abs(rankingState));
				if(rankingState > 0){
					this._ranking_icon.gotoAndStop(2);	//상승 아이콘 (붉은색)
				}else if(rankingState < 0){
					this._ranking_icon.gotoAndStop(1);	//하강 아이콘 (파란색)
				}else if(rankingState == 0){
					this._ranking_icon.gotoAndStop(3);	//하강 아이콘 (파란색)
				}

			}
			this._friend_on.friend_txt.txt.text=VONullObject.getLoadInstance().nullNickText($obj.cntfriend);
			
		}
		
		private function removeData():void
		{
			this._level.level_txt = this._name.text = this._score.text = this._win.text = this._lose.text = this._grade.text = this._ranking.text = this._ranking_grade.text = "";

			this._ranking_icon.gotoAndStop(1);
			this._level.gotoAndStop(1);
	
		}
		
		private function onClickHandler(e:MouseEvent):void
		{
			//var obj:Object = {};
			//click한 info버튼을 미디에이터(Mediator_infobar)로.
			switch(e.currentTarget){
				case this._infoBtn :
					//trace('click, info');
					//DAOSendFlash.getLoadInstance().setData("initSubjectList", obj);
					this.dispatchEvent(new EClick('info', EClick.EVENT_CLICK));
				break;
				case this._friendBtn :
					//trace('click, friend');
					//DAOSendFlash.getLoadInstance().setData("initFriendAdmin", obj);
					this.dispatchEvent(new EClick('friend', EClick.EVENT_CLICK));
				break;
				case this._settingBtn :
					//trace('click, setting');
					//DAOSendFlash.getLoadInstance().setData("initSetting", obj);
					this.dispatchEvent(new EClick('setting', EClick.EVENT_CLICK));
				break;
			}
		}
	}
}